package com.yanqu.road.dao.impl.logic;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.DbInfo;
import com.yanqu.road.utils.string.StringUtils;

public class DbInfoParser {
    public static DbInfo parseDbInfo(String str) {
        if (StringUtils.isNullOrEmpty(str)) {
            return null;
        }

        JSONObject jObject = null;
        try {
            jObject = JSONObject.parseObject(str);
            DbInfo info = new DbInfo();
            info.setDbName(jObject.getString("dbname"));
            info.setUserName(jObject.getString("dbuser"));
            info.setIp(jObject.getString("ip"));
            info.setPassword(jObject.getString("dbpassword"));
            info.setPort(jObject.getIntValue("port"));
            info.setConnectionNum(jObject.getIntValue("dbconnection"));
            if (jObject.containsKey("partitionCount")) {
                info.setPartitionCount(jObject.getInteger("partitionCount"));
            }
            if (jObject.containsKey("maxConnectionsPerPartition")) {
                info.setMaxConnectionsPerPartition(jObject.getInteger("maxConnectionsPerPartition"));
            }
            if (jObject.containsKey("minConnectionsPerPartition")) {
                info.setMinConnectionsPerPartition(jObject.getInteger("minConnectionsPerPartition"));
            }
            return info;
        } catch (Exception e) {
            return null;
        }
    }
}
